package us.wili.dev.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Created by JasonY on 17/5/29.
 */
public class ApiJsonSerializeUtil {

    private static final Integer DEFAULT_CODE = 200;

    public static Map<String, Object> genStatus(String message, Integer code) {
        Map<String, Object> status = new HashMap<String, Object>();

        code = code == null ? DEFAULT_CODE : code;
        status.put("errCode", code);

        if (message != null) {
            status.put("message", message);
        }

        return status;
    }

    public static Map<String, Object> genResponse(Object data, String message, Integer code) {
        Map<String, Object> response = new HashMap<String, Object>();
        response.put("status", genStatus(message, code));

        if (data != null) {
            response.put("data", data);
        }

        return response;
    }

    public static Map<String, Object> genResponse(String message, Integer code) {
        return genResponse(null, message, code);
    }

    public static Map<String, Object> genResponse(Object data) {
        return genResponse(data, null, null);
    }

    public static Map<String, Object> genResponse() {
        return genResponse(null, null, null);
    }

    public static String jsonResponse(Object data, String message, Integer code) {
        return JSON.toJSONString(genResponse(data, message, code), SerializerFeature.DisableCircularReferenceDetect);
    }

    public static String jsonResponse() {
        return JSON.toJSONString(genResponse());
    }

    public static String jsonResponse(String message, Integer code) {
        return jsonResponse(null, message, code);
    }

    public static String jsonResponse(Object data) {
        return jsonResponse(data, null, null);
    }

    public static String jsonResponseWithInclude(Object data, Set<String> includes, String message, Integer code) {
        SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
        Set<String> filterExcludes = filter.getIncludes();
        filterExcludes.addAll(includes);

        return JSON.toJSONString(genResponse(data, message, code), filter);
    }

    public static String jsonResponseWithInclude(Object data, Set<String> includes) {
        return jsonResponseWithInclude(data, includes, null, null);
    }

    public static String jsonResponseWithExcludes(Object data, Set<String> excludes, String message, Integer code){
        SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
        Set<String> filterExcludes = filter.getExcludes();
        filterExcludes.addAll(excludes);

        return JSON.toJSONString(genResponse(data, message, code), filter);
    }

    public static String jsonResponseWithExcludes(Object data, Set<String> excludes){
        return jsonResponseWithExcludes(data, excludes, null, null);
    }

    public static Set<String> stringsToSet(String... params) {
        Set<String> includes = new HashSet<>();

        for (String param : params) {
            includes.add(param);
        }

        return includes;
    }
}
